package com.googlecode.stockhamster.rumor;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import com.googlecode.stockhamster.Constant;
import com.googlecode.stockhamster.company.ICompany;
import com.googlecode.stockhamster.stockstat.IStockStat;

public class RumorServiceImpl implements IRumor {
	
	private	ICompany	company;
	private	IStockStat		status;
	
	public RumorServiceImpl() {
		XFireProxyFactory stateFactory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
		Service stateSrvcModel = new ObjectServiceFactory().create(IStockStat.class);
		String stateURL = Constant.stockStatUrl;		
		try {
			status = (IStockStat)stateFactory.create(stateSrvcModel, stateURL);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		XFireProxyFactory companyFactory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
		Service companySrvcModel = new ObjectServiceFactory().create(ICompany.class);
		String companyURL = Constant.companyUrl;		
		try {
			company = (ICompany)companyFactory.create(companySrvcModel, companyURL);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	public Double getRumorEffectRatioByBank1(Integer rumorID) {
		Rumor rumor = Rumor.get(rumorID);
		return rumor.getRatio1();
	}
	
	public Double getRumorEffectRatioByBank2(Integer rumorID) {
		Rumor rumor = Rumor.get(rumorID);
		return rumor.getRatio2();
	}
	
	public Double getRumorEffectRatioByBank3(Integer rumorID) {
		Rumor rumor = Rumor.get(rumorID);
		return rumor.getRatio3();
	}

	@Override
	public Integer[] getRumorEffectStocks(Integer rumorID) {
		Rumor rumor = Rumor.get(rumorID);
		
		if (rumor.isEffectCategory()) {
			return	status.getCategoryStocks(rumor.getEffectCategory());
		} else {
			Integer tarStock = status.getStockId(rumor.getEffectCompany());
			if (tarStock != null) {
				Integer[] result = new Integer[1];
				result[0] = tarStock;
				return result;
			} else {
				Integer[] result = new Integer[0];
				return result;
			}			
		}
	}

	@Override
	public Integer[] getRumorList(Integer startID) {		
		return	Rumor.getRumors(startID);
	}

	@Override
	public Boolean makeRumorCategory(String adminCode, String category, Double effectRatio,
			Double ratioByBank1, Double ratioByBank2, Double ratioByBank3) {
		
		if (! adminCode.equals(Constant.rumorAdminCode))
			return false;
		
		Rumor	rumor = new Rumor(category, null, effectRatio,
				ratioByBank1, ratioByBank2, ratioByBank3);
		rumor.insert();
		
		IncRatio	cateRatio = IncRatio.get(null, category);
		if (cateRatio == null) {
			cateRatio = new IncRatio(null, category, ratioByBank1, ratioByBank2, ratioByBank3);
			return cateRatio.insert();
		} else {
			cateRatio.ratio1 += ratioByBank1;
			cateRatio.ratio2 += ratioByBank2;
			cateRatio.ratio3 += ratioByBank3;			
			return cateRatio.save();
		}	
	}

	@Override
	public Boolean makeRumorCompany(String adminCode, String companyName, Double effectRatio,
			Double ratioByBank1, Double ratioByBank2, Double ratioByBank3) {
		
		if (! adminCode.equals(Constant.rumorAdminCode))
			return false;
		
		Rumor	rumor = new Rumor(null, companyName, effectRatio,
				ratioByBank1, ratioByBank2, ratioByBank3);
		rumor.insert();
		
		IncRatio	compRatio = IncRatio.get(companyName, null);
		if (compRatio == null) {
			compRatio = new IncRatio(companyName, null, ratioByBank1, ratioByBank2, ratioByBank3);
			compRatio.insert();
		} else {
			compRatio.ratio1 += ratioByBank1;
			compRatio.ratio2 += ratioByBank2;
			compRatio.ratio3 += ratioByBank3;			
			compRatio.save();
		}
		
		return company.rumorEffectCompany(companyName, effectRatio);
	}

	public Boolean updateRatio(String adminCode, String companyName, Double ratio) {
		if (adminCode.equals(Constant.rumorAdminCode)) {
			IncRatio	compRatio = IncRatio.get(companyName, null);
			if (compRatio == null) {
				compRatio = new IncRatio(companyName, null, ratio, ratio, ratio);
				
				return compRatio.insert();
			} else {
				compRatio.ratio1 = compRatio.ratio2 = compRatio.ratio3 = ratio;
				return compRatio.save();
			}
		}
		return false;
	}
	
	public Double getCompanyRatio(Integer bankIndex, String companyName) {
		IncRatio	compRatio = IncRatio.get(companyName, null);
		if (compRatio == null) return (double)0.0;
		if (bankIndex == 1) return compRatio.ratio1;
		if (bankIndex == 2) return compRatio.ratio2;
		if (bankIndex == 3) return compRatio.ratio3;
		return null;
	}
	
	public Double getCategoryRatio(Integer bankIndex, String category) {
		IncRatio	cateRatio = IncRatio.get(null, category);
		if (cateRatio == null) return (double)0.0;
		if (bankIndex == 1) return cateRatio.ratio1;
		if (bankIndex == 2) return cateRatio.ratio2;
		if (bankIndex == 3) return cateRatio.ratio3;
		return null;
	}
}
